package projet;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import projet.dataaccess.DataAccess;
import projet.dataaccess.JSON;
import projet.dataaccess.PathErrorException;
import projet.item.Item;

/**
 * Class that represends a classic User..
 * 
 * @author CORSINI Pierre, Cyril CHANDELIER, Sylvain BRUN
 * @version January 25, 2010
 */
public class User {
	private ArrayList<Integer> friendsIDs = new ArrayList<Integer>();
	private Integer id;
	private ArrayList<Item> items = new ArrayList<Item>();
	private String passwd;
	private String username;

	/**
	 * Create User
	 * 
	 * @param id His id
	 * @param n His user name
	 * @param p His password
	 */
	public User(int id, String n, String p) {
		this.id = id;
		this.username = n;
		this.passwd = Tools.Encode(p);
	}

	/**
	 * Create User from JSONObject
	 * 
	 * @param jsonObject The JSONObject
	 * @throws JSONException
	 */
	public User(JSONObject jsonObject) throws JSONException {
		this.id = jsonObject.getInt("id");
		this.username = jsonObject.getString("username");
		this.passwd = jsonObject.getString("passwd");

		for (String i : jsonObject.getString("friends").split(", "))
			if (i.length() != 0)
				this.friendsIDs.add(Integer.parseInt(i));

	}

	/**
	 * Add a fiend to the user
	 * 
	 * @param u To friend to add
	 */
	public void addFriend(User u) {
		if (!this.friendsIDs.contains(u.id))
			this.friendsIDs.add(u.id);
	}

	/**
	 * Add an item to the user
	 * 
	 * @param i The item
	 */
	public void addItem(Item i) {
		this.items.add(i);
	}

	/**
	 * Change his pwd
	 * 
	 * @param p The pwd
	 */
	public void changePsswd(String p) {
		this.passwd = Tools.Encode(p);
	}

	/**
	 * Change his username
	 * 
	 * @param p The new username
	 */
	public void changeUsername(String u) {
		this.username = u;
	}

	/**
	 * Get his friends
	 * 
	 * @return His friends
	 */
	public ArrayList<Integer> getFriendIDs() {
		return this.friendsIDs;
	}

	/**
	 * Get the id
	 * 
	 * @return The id
	 */
	public int getId() {
		return this.id;
	}

	/**
	 * Get his items
	 * 
	 * @return His items
	 */
	public ArrayList<Item> getItems() {
		return this.items;
	}

	/**
	 * Get the user name
	 * 
	 * @return The user name
	 */
	public String getUsername() {
		return this.username;
	}

	/**
	 * Test if the user own a item
	 * 
	 * @param i The item
	 * @return True if he has it
	 */
	public boolean haveItem(int i) {
		return this.items.contains(i);
	}

	public boolean isFriendWith(User u) {
		return this.friendsIDs.contains(u.getId())
		&& u.getFriendIDs().contains(this.getId());
	}

	/**
	 * Test the given string is the password
	 * 
	 * @param s The string
	 * @return s == the password
	 */
	public boolean PasswordEquals(String s) {
		return Tools.Encode(s).equals(this.passwd);
	}

	/**
	 * Remove a firned to the user
	 * 
	 * @param u The friend to remove
	 */
	public void removeFriend(User u) {
		if (this.friendsIDs.contains(u.getId()))
			this.friendsIDs.remove((Object) u.getId());
	}

	/**
	 * Remove an item to the user
	 * 
	 * @param id The item id
	 */
	public void removeItem(int id) {
		for (Item i : this.items)
			if (i.getId() == id)
				this.removeItem(i);
	}

	/**
	 * Remove an item to the user
	 * 
	 * @param i The item
	 */
	public void removeItem(Item i) {
		this.items.remove(i);
	}

	/**
	 * Save the User into a JSONObject
	 * 
	 * @return The JSONObject we obtain
	 * @throws PathErrorException
	 */
	public DataAccess toDataAccess() throws PathErrorException {
		JSON da = new JSON();
		da.addElement("id", this.id.toString());
		da.addElement("username", this.username);
		da.addElement("passwd", this.passwd);
		da.addElement("friends", this.friendsIDs.toString().substring(1,
				this.friendsIDs.toString().length() - 1));

		return da;
	}


	@Override
	public String toString() {
		return this.username + ":  Password:" + this.passwd
		+ " Number of items: " + this.items.size()
		+ "Number of friends: " + this.friendsIDs.size();
	}

	/**
	 * ToString, but only public informations
	 * 
	 * @return The string
	 */
	public String toStringPublic() {
		return this.username + ": " + this.items.size() + " items, "
		+ this.friendsIDs.size() + " friends ";
	}

}
